/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ma.projet.managedBean;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import ma.projet.entities.Incident;
import ma.projet.entities.Soustraitant;
import ma.projet.inter.IncidentWeek;
import ma.projet.inter.Week;
import ma.projet.local.IncidentFacadeLocal;
import ma.projet.local.SoustraitantFacadeLocal;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;

/**
 *
 * @author Maison
 */
@ManagedBean
@RequestScoped
public class ChartBean {

    /**
     * Creates a new instance of ChartBean
     */
    private CartesianChartModel IncidentsWeek;
    private CartesianChartModel IncidentsDate;
    private CartesianChartModel IncidentsSoutraitantWeek;
    private List<Soustraitant> soustraitants;
    private CartesianChartModel categoryModel;
    private PieChartModel pieModel;
    private List<Week> listweek;
    private int[][] liste;
    @EJB
    private IncidentFacadeLocal ifl;
    @EJB
    private SoustraitantFacadeLocal sfl;

    public int[][] getListe() {
        return liste;
    }

    public void setListe(int[][] liste) {
        this.liste = liste;
    }

    public List<Week> getListweek() {
        return listweek;
    }

    public void setListweek(List<Week> listweek) {
        this.listweek = listweek;
    }

    private int min;

    public PieChartModel getPieModel() {
        return pieModel;
    }

    public void setPieModel(PieChartModel pieModel) {
        this.pieModel = pieModel;
    }

    public Date getDebut() {
        return debut;
    }

    public void setDebut(Date debut) {
        this.debut = debut;
    }

    public Date getFin() {
        return fin;
    }

    public void setFin(Date fin) {
        this.fin = fin;
    }
    private int max;
    private int year;
    private Date debut;
    private Date fin;

    public CartesianChartModel getIncidentsDate() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        IncidentsDate = new CartesianChartModel();
        Calendar c = Calendar.getInstance();
        c.setTime(debut);
        ChartSeries Day = new ChartSeries();
        Day.setLabel("Incident Par Date");
        for (int i = 0;; i++) {
            Day.set(df.format(c.getTime()), ifl.NbrIcidentDay(c.getTime()));
            c.add(Calendar.DAY_OF_MONTH, 1);

            if (fin.equals(c.getTime())) {
                break;
            }

        }

        IncidentsDate.addSeries(Day);

        return IncidentsDate;
    }

    public void setIncidentsDate(CartesianChartModel IncidentsDate) {
        this.IncidentsDate = IncidentsDate;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMin() {
        return min;
    }

    public void setMin(int id) {
        this.min = id;
    }

    public int s;

    @PostConstruct
    public void reset() {

        s = getSoustraitants().size();
    }

    public ChartBean() {
        max = 52;
        min = 0;
        year = 2014;

        DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            debut = dfm.parse("2014-02-06 00:00:00");
            fin = dfm.parse("2014-02-14 00:00:00");

        } catch (ParseException ex) {

        }
        liste = new int[8][53];
        lw=new ArrayList<IncidentWeek>();
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public CartesianChartModel getCategoryModel() {
        getSoustraitants();

        categoryModel = new CartesianChartModel();
        ChartSeries sous = new ChartSeries();
        sous.setLabel("Sous-Traitant");
        for (Soustraitant s : soustraitants) {
            sous.set(s.getLibelle(), s.getIncidentList().size());
        }
        categoryModel.addSeries(sous);
        return categoryModel;
    }

    public List<Soustraitant> getSoustraitants() {
        if (soustraitants == null) {
            soustraitants = sfl.findAll();
        }
        return soustraitants;
    }

    public void setSoustraitants(List<Soustraitant> soustraitants) {
        this.soustraitants = soustraitants;
    }
    
    private List<IncidentWeek> lw;
    public List<IncidentWeek> getLw() {
        remplir();
        return lw;
    }
    public CartesianChartModel remplir() {
        lw=new ArrayList<IncidentWeek>();
        IncidentsWeek = new CartesianChartModel();
        ChartSeries week = new ChartSeries();
        week.setLabel("Incident Par Week");
        for (int i = min; i < max; i++) {
            week.set("W" + (i + 1), ifl.NbrIcidentWeek(week(i).get(0), week(i).get(1)));
           lw.add(new IncidentWeek("W"+(i+1),ifl.NbrIcidentWeek(week(i).get(0), week(i).get(1))));
        }

        IncidentsWeek.addSeries(week);
        return IncidentsWeek;
    }

    public CartesianChartModel getIncidentsWeek() {
        remplir();
        return IncidentsWeek;
    }

    public String getDatatipFormat() {
        return "<span style=\"display:none;\">%s</span><span>%.0f</span>";
    }

    public void setIncidentsWeek(CartesianChartModel IncidentsWeek) {
        this.IncidentsWeek = IncidentsWeek;
    }

    public List<Date> week(int week) {
        Calendar c = Calendar.getInstance();

     //   c.setFirstDayOfWeek(Calendar.FRIDAY);
//     System.out.println(c.get(Calendar.WEEK_OF_YEAR));
        c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
        c.set(Calendar.WEEK_OF_YEAR, week);
      // 
        c.clear(Calendar.MINUTE);
        c.clear(Calendar.SECOND);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.YEAR, year);
        List<Date> dates = new ArrayList<Date>();
        dates.add(c.getTime());
        System.out.println(c.getTime());
        c.add(Calendar.DAY_OF_MONTH, 7);
        System.out.println(c.getTime());
        dates.add(c.getTime());
        return dates;

    }

    public CartesianChartModel getIncidentsSoutraitantWeek() {
        pieModel = new PieChartModel();

        int somme = 0;
        int n = 0, j = 0;

        IncidentsSoutraitantWeek = new CartesianChartModel();
        for (Soustraitant s : sfl.findAll()) {
            String sous = s.getLibelle();
            listweek = new ArrayList<Week>();
            ChartSeries Bar = new ChartSeries();
            List<Integer> lw = new ArrayList<Integer>();
            Bar.setLabel(s.getLibelle());
            n = min;

            for (int i = min; i < max; i++) {
                Bar.set("W" + (i + 1), ifl.NbrIcidentWeeksous(week(i).get(0), week(i).get(1), s.getSoustraitantID()));
                somme += ifl.NbrIcidentWeeksous(week(i).get(0), week(i).get(1), s.getSoustraitantID());
                listweek.add(new Week(i + 1));

                liste[j][n] = ifl.NbrIcidentWeeksous(week(i).get(0), week(i).get(1), s.getSoustraitantID());
                n++;
                //System.out.println("----------"+liste[n][j]);
            }
            j++;
            pieModel.set(s.getLibelle(), somme);
            somme = 0;

            IncidentsSoutraitantWeek.addSeries(Bar);
        }
        

        return IncidentsSoutraitantWeek;
    }

    public void setIncidentsSoutraitantWeek(CartesianChartModel IncidentsSoutraitantWeek) {
        this.IncidentsSoutraitantWeek = IncidentsSoutraitantWeek;
    }

}
